//
//  YAEmoticonPackage.swift
//  表情键盘[练习]
//
//  Created by 鱼龙之梦 on 15/9/11.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit

class YAEmoticonPackage: NSObject {
///  目录名
    var id:String?
///  分组名
    var group_name_cn:String?
    lazy var emoticons = [YAEmoticon]()
    init(keyedValues: [String : AnyObject]) {
        super.init()
        //setValuesForKeysWithDictionary(keyedValues) 赋值顺序乱序
        id = keyedValues["id"] as? String
        group_name_cn = keyedValues["group_name_cn"] as? String
        //注意：每隔20个按钮添加一个删除按钮(注意每组21个表情，数组的20就是第21个位置)
        var index = 0
        if let array = keyedValues["emoticons"] as? [[String:String]] {
            for var d in array{
                if let imagePath = d["png"]{
                    d["png"] = id! + "/" + imagePath
                }
                emoticons.append(YAEmoticon(keyedValues: d))
                index++
                if index == 20 {//防止删除按钮
                   emoticons.append(YAEmoticon(isRemove: true))
                   index = 0
                }
            }
        }
        //添加完后再做判断
        appendBlankEmoticon()
    }
    func appendBlankEmoticon(){
        let count = emoticons.count % 21
        
        if count == 0 && emoticons.count > 0{
            return //说明没有要添加空位置的必要
        }
        for _ in count..<20{
            emoticons.append(YAEmoticon(isEmpty: true))
        }
        emoticons.append(YAEmoticon(isRemove: true))
    }
    override var description:String{
        let keys = ["id","group_name_cn","emoticons"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
